package com.linchong.lock.controller;




import com.linchong.lock.dao.DistributeLockMapper;
import com.linchong.lock.model.DistributeLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
 * @author linchong
 * @version 1.0
 * @Date: 2020-12-06 14:49
 * @Description: DemoController$
 */
@Slf4j
@RestController
public class DemoController {

	@Resource
	private DistributeLockMapper distributeLockMapper;

	@GetMapping("singleLock")
	@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
	public String singleLock() throws Exception {
		log.info("进入了方法...");
		DistributeLock distributeLock = distributeLockMapper.selectDistributeLock("demo");
		if(distributeLock==null){
			throw new Exception("分布式锁找不到！");
		}
		log.info("进入锁的内部...");
		try {
			Thread.sleep(30_000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return "执行完成！";
	}

}
